package Cau12;

import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class ServerMahoa extends Thread implements ActionListener {

	private JFrame frmServeruploadsinhcoi;
	JButton btnStart;
	Thread t;
	private JButton btnStop;
	private JLabel lbtrangthai;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerMahoa window = new ServerMahoa();
					window.frmServeruploadsinhcoi.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerMahoa() {
		initialize();
		try {
			LocateRegistry.createRegistry(1099);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmServeruploadsinhcoi = new JFrame();
		frmServeruploadsinhcoi.setTitle("Server_CryptographyFile_Sinh_coi");
		frmServeruploadsinhcoi.setBounds(100, 100, 403, 141);
		frmServeruploadsinhcoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmServeruploadsinhcoi.getContentPane().setLayout(null);
		
		btnStart = new JButton("Start");
		btnStart.addActionListener(this);
			
		btnStart.setBounds(61, 22, 89, 47);
		frmServeruploadsinhcoi.getContentPane().add(btnStart);
		
		btnStop = new JButton("Stop");
		btnStop.setEnabled(false);
		btnStop.setBounds(203, 22, 89, 47);
		btnStop.addActionListener(this);
		frmServeruploadsinhcoi.getContentPane().add(btnStop);
		
		lbtrangthai = new JLabel("");
		lbtrangthai.setBounds(36, 80, 280, 14);
		frmServeruploadsinhcoi.getContentPane().add(lbtrangthai);
	}
	public void run(){
		try {
			//UploadImpl ui=new UploadImpl(txtpath.getText());
			mahoaImpl mi=new mahoaImpl();
			Naming.rebind("rmi://localhost/Servermahoa", mi);
			lbtrangthai.setText("server bat dau");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent ex) {
		if(ex.getSource()==btnStart){
			t=new Thread(this);
			btnStart.setEnabled(false);
			btnStop.setEnabled(true);
			t.start();
;
		}
		if(ex.getSource()==btnStop)
		{
			try {
				Naming.unbind("rmi://localhost/Servermahoa");
				btnStart.setEnabled(true);
				btnStop.setEnabled(false);
				lbtrangthai.setText("Server Stop");
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

}
